package com.dmcloud.system.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dmcloud.common.base.BaseServiceImpl;
import com.dmcloud.system.dto.UserInfo;
import com.dmcloud.system.entity.SysUser;
import com.dmcloud.system.mapper.SysUserMapper;
import com.dmcloud.system.service.ISysRoleService;
import com.dmcloud.system.service.ISysUserService;
import com.dmcloud.system.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaodao
 * @since 2019-10-30
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private ISysRoleService sysRoleService;

    @Override
    public UserVO getUserVoById(String id) {
        return baseMapper.findAllRoleByUserId(id);
    }

    /***
     * 查询用户的权限,和角色
     * @param user
     * @return
     */
    @Override
    public UserInfo getUserInfo(SysUser user) {
        UserInfo userInfo = new UserInfo();
        userInfo.setSysUser(user);
        List<String> roleIds = sysRoleService.findRolesByUserId(user.getId())
                .stream().map(sysRole -> sysRole.getRoleCode()).collect(Collectors.toList());
        userInfo.setRoles(ArrayUtil.toArray(roleIds, String.class));
        return userInfo;
    }

    @Override
    public IPage getAllUser(Page page, SysUser user) {
        return baseMapper.getAllUser(page, user);
    }

    @Override
    public boolean save(SysUser entity) {
        return super.save(entity);
    }
}
